<template>
    <div class="divShow">
        <el-row>
            <el-col :span="24">
                <div style="padding: 30px;">
                    <!-- 搜索框 -->
                    <el-form :inline="true" :model="searchWorkstation" class="demo-form-inline" v-if="showSearch">
                        <el-form-item label="工作站编码">
                            <el-input v-model="searchWorkstation.workstationCode" placeholder="请输入工作站编码"></el-input>
                        </el-form-item>
                        <el-form-item label="工作站名称">
                            <el-input v-model="searchWorkstation.workstationName" placeholder="请输入工作站名称"></el-input>
                        </el-form-item>
                        <el-form-item label="所在车间名称">
                            <el-input v-model="searchWorkstation.workshopName" placeholder="请输入所在车间名称"></el-input>
                        </el-form-item>
                        <el-form-item label="所属工序">
                            <el-input v-model="searchWorkstation.processName" placeholder="请输入所属工序"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="checkWorkstation">查询</el-button>
                            <el-button @click="cleanWorkstation">重置</el-button>
                        </el-form-item>
                    </el-form>


                    <el-row>
                        <el-col :span="22">
                            <!-- 按钮 -->
                            <el-button type="primary" size="small" @click="changeDialog(null)">新增</el-button>
                            <el-button type="success" size="small" :disabled="!judgmentCh"
                                @click="changeDialog(SelWorkstationId)">修改</el-button>
                            <el-button type="danger" size="small" :disabled="!judgmentDe"
                                @click="delJudg">删除</el-button>
                        </el-col>
                        <el-col :span="2">
                            <div>
                                <el-tooltip content="隐藏搜索" placement="top">
                                    <el-button icon="el-icon-search" circle @click="toggleSearch"></el-button>
                                </el-tooltip>
                                <el-tooltip content="刷新" placement="top">
                                    <el-button icon="el-icon-refresh" circle @click="refresh"></el-button>
                                </el-tooltip>
                            </div>
                        </el-col>
                    </el-row>
                    <!-- 表格 -->
                    <el-table :data="workstationTable" ref="WorkstationTable" style="width: 100%;"
                        @selection-change="handleSelectionChange">

                        <el-table-column type="selection" width="55">
                        </el-table-column>
                        <el-table-column prop="workstationId" label="工作站ID" v-if="false">
                        </el-table-column>
                        <el-table-column prop="workstationCode" label="工作站编号">
                        </el-table-column>
                        <el-table-column prop="workstationName" label="工作站名称">
                        </el-table-column>
                        <el-table-column prop="workstationAddress" label="工作站地点">
                        </el-table-column>
                        <el-table-column prop="workshopName" label="所在车间名称">
                        </el-table-column>
                        <el-table-column prop="processName" label="所属工序">
                        </el-table-column>
                        <el-table-column prop="enableFlag" label="是否启用">
                            <template slot-scope="scope">
                                <el-tag v-if="scope.row.enableFlag === 'Y'">是</el-tag>
                                <el-tag v-else type="danger">否</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column prop="remark" label="备注">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-col :span="12">
                                    <el-link type="primary" @click="changeDialog(scope.row.workstationId)">修改</el-link>
                                </el-col>
                                <el-col :span="12">
                                    <el-link type="primary" @click="delDialog(scope.row.workstationId)">删除</el-link>
                                </el-col>
                            </template>
                        </el-table-column>
                    </el-table>
                    <!-- 分页模块 -->
                    <el-pagination :page-size="searchWorkstation.pageSize" :total="searchWorkstation.total"
                        @current-change="searchWorkstationChange" layout="prev, pager, next">
                    </el-pagination>

                    <!-- dialog -->
                    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" width="30%" class="rounded-dialog"
                        :close-on-click-modal="false" @close="closeWorkstationDialog">
                        <el-form :model="AddWorkstation" ref="workstationForm" :rules="rules">

                            <el-form-item label="工作站编号" prop="workstationCode">
                                <el-input v-model="AddWorkstation.workstationCode" autocomplete="off"
                                    style="width: auto;"></el-input>
                            </el-form-item>
                            <el-form-item label="工作站名称" prop="workstationName">
                                <el-input v-model="AddWorkstation.workstationName" autocomplete="off"
                                    style="width: auto;"></el-input>
                            </el-form-item>
                            <el-form-item label="工作站地点" prop="workstationAddress">
                                <el-input-number v-model="AddWorkstation.workstationAddress" :min="1" :max="200"
                                    label="描述文字" style="width: auto;"></el-input-number>
                            </el-form-item>
                            <el-form-item label="所在车间" prop="workshopId">
                                <el-select v-model="AddWorkstation.workshopId" placeholder="请选择车间">
                                    <el-option v-for="item in workshopOptions" :key="item.value" :label="item.label"
                                        :value="item.value">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="所属工序" prop="processId">
                                <el-select v-model="AddWorkstation.processId" placeholder="请选择车间">
                                <el-option v-for="item in processOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                            </el-form-item>
                            <el-form-item label="是否启用" prop="enableFlag">
                                <el-radio v-model="AddWorkstation.enableFlag" label="Y">是</el-radio>
                                <el-radio v-model="AddWorkstation.enableFlag" label="N">否</el-radio>
                            </el-form-item>
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="AddWorkstation.remark" autocomplete="off"
                                    style="width: auto;"></el-input>
                            </el-form-item>

                        </el-form>
                        <div slot="footer" class="dialog-footer">
                            <el-button @click="closeWorkstationDialog">取 消</el-button>
                            <el-button type="primary" @click="submitForm('workstationForm')">确 定</el-button>
                        </div>
                    </el-dialog>
                </div>
            </el-col>
        </el-row>

    </div>
</template>
<script>
import axios from 'axios';
import qs from 'qs';
import * as XLSX from 'xlsx'; // 使用命名导入
import FileSaver from 'file-saver';
import excelExport from '../../utils/excel-export';
export default {
    data() {
        return {
            workshopOptions: [], // 存储所有车间选项
            processOptions: [], // 存储所有车间选项
            // 设置dialog默认不显示
            dialogFormVisible: false,
            //根据是否有id输入判断标题
            dialogTitle: '',
            // 分页查询数据
            searchWorkstation: {
                workstationCode: '',
                workstationName: '',
                workshopName: '',
                processName: '',
                pageSize: 5,
                pageNo: 1,
                total: 0,
            },
            // 查询显示表格
            workstationTable: [],
            //新增AddWorkshop或者修改AddWorkshop
            AddWorkstation: {
                workstationId: '',
                workstationCode: '',
                workstationName: '',
                workstationAddress: '',
                workshopId: '',
                processId: '',
                enableFlag: '',
                remark: '',
            },
            // 存储所有选中的行
            selectedRows: [],
            judgmentCh: false,
            judgmentDe: false,
            //当前选中行的vendorId的数组
            arrayWorkstationId: [],
            // 获取当前选中行的vendorId
            SelWorkstationId: '',
            showSearch: true, // 控制搜索框的显示和隐藏
            columns: [
                'workshopCode',
                'workshopName',
                'area',
                'charge',
                'enableFlag',
                'remark"',
            ],
            rules: {
                workstationCode: [
                    { required: true, message: '请输入工作站编码', trigger: 'blur' },
                    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
                ],
                workstationName: [
                    { required: true, message: '请输入工作站名称', trigger: 'blur' },
                    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
                ],
                processId: [
                    { required: true, message: '请选择工序', trigger: 'change' },
                ],
                workshopId: [
                    { required: true, message: '请选择车间', trigger: 'change' },
                ],
                enableFlag: [
                    { required: true, message: '请选择是否启用', trigger: 'change' }
                ]
            }
        }
    },

    mounted() {
        this.checkListWorkstation();
        this.checkListWorkshop();
        this.checkListProcess();
    },
    methods: {
        // 查询process数据方法
        checkListProcess() {
            this.$axios.post("http://localhost:9970/emsSystem/api/process/processCheckAll").then((res) => {
                if (res.data.code === 200) {
                    if (res.data.code === 200) {
                        // 假设返回的数据是一个包含多个工序对象的数组
                        this.processOptions = res.data.data.map(item => ({
                            value: item.processId, // 使用正确的唯一标识符
                            label: item.processName // 使用正确的名称字段
                        }));
                    }
                }
            })
        },
        // 查询Workshop数据方法
        checkListWorkshop() {
            this.$axios.post("http://localhost:9970/emsSystem/api/workshop/workshopCheckAll").then((res) => {
                if (res.data.code === 200) {
                    if (res.data.code === 200) {
                        // 假设返回的数据是一个包含多个车间对象的数组
                        this.workshopOptions = res.data.data.map(item => ({
                            value: item.workshopId, // 使用正确的唯一标识符
                            label: item.workshopName // 使用正确的名称字段
                        }));
                    }
                }
            })
        },
        //刷新按钮
        refresh() {
            location.reload();
        },
        //是否显示搜索框
        toggleSearch() {
            // 切换搜索框的显示和隐藏
            this.showSearch = !this.showSearch;
        },
        //删除判断
        delJudg() {
            if (this.selectedRows.length === 1) {
                //删除单个id
                this.delDialog(this.SelWorkstationId)
            } else {
                //删除多个id
                console.log(this.arrayWorkstationId);
                this.$confirm("确定要删除" + this.arrayWorkstationId + " 这" + this.arrayWorkstationId.length + "条数据吗？", '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.delWorkshopIdArray(this.arrayWorkstationId)
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
        },
        //获取当前选中表格行的状态
        handleSelectionChange(selection) {
            this.selectedRows = selection;
            // 更新 arrayWorkstationId
            this.arrayWorkstationId = selection.map(row => row.workstationId);

            if (selection.length === 1) {
                //获取单个id
                this.SelWorkstationId = this.selectedRows[0].workstationId
                console.log(this.SelWorkstationId);

                this.judgmentDe = true;
                this.judgmentCh = true;
            } else if (selection.length > 1) {
                //获取一组id
                let rowsLength = this.selectedRows.length
                for (let index = 0; index < rowsLength; index++) {
                    this.arrayWorkstationId[index] = this.selectedRows[index].workstationId;
                }
                this.judgmentCh = false;
                this.judgmentDe = true;
            } else {
                this.judgmentCh = false;
                this.judgmentDe = false;
            }
        },
        //根据传递进来的MdItemId数组来进行批量删除
        delWorkshopIdArray(arrayWorkstationId) {
            this.$axios.delete("http://localhost:9970/emsSystem/api/workstation/delWorkstationIdArray", { data: arrayWorkstationId }).then((res) => {
                if (res.data.code === 200) {
                    //删除成功
                    this.$message.success("删除成功");
                    this.checkListWorkstation();
                }
            })
        },
        //根据id删除指定数据
        delDialog(workstationId) {
            this.$confirm("确定要删除该编号为 " + workstationId + " 的信息, 是否继续?", '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$axios.delete("http://localhost:9970/emsSystem/api/workstation/delWorkstation/" + workstationId).then((res) => {
                    if (res.data.code === 200) {
                        //删除成功
                        this.$message.success("删除成功");
                        this.checkListWorkstation();
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //关闭dialog并且清空里面的数据
        closeWorkstationDialog() {
            this.$refs.workstationForm.resetFields();
            this.AddWorkstation.workstationId = "",
                this.dialogFormVisible = false
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // 表单验证通过，执行提交操作
                    this.AddOrChWorkstation();
                } else {
                    console.log('表单验证失败');
                    return false;
                }
            });
        },
        //添加或者修改Workstation
        AddOrChWorkstation() {
            let qsWorkstation = qs.stringify(this.AddWorkstation);
            this.$axios.post("http://localhost:9970/emsSystem/api/workstation/AddOrChWorkstation", qsWorkstation).then((res) => {
                if (res.data.code === 200) {
                    //修改成功
                    this.$message.success("修改或新增成功");
                    this.dialogFormVisible = false;
                    this.checkListWorkstation();
                }
            })
        },
        //改变dialog显示状态
        changeDialog(id) {
            this.dialogFormVisible = true
            if (id == null) {
                this.dialogTitle = "添加车间"
            } else {
                this.dialogTitle = "修改车间"
                this.AddWorkstation.workstationId = id
                this.checkWorkstationById(id);
            }

        },
        //根据workshopId查询数据
        checkWorkstationById(workstationId) {
            this.$axios.post("http://localhost:9970/emsSystem/api/workstation/checkWorkstationById/" + workstationId).then((res) => {
                if (res.data.code === 200) {
                    this.AddWorkstation = res.data.data
                }
            })
        },
        // 改变页码
        searchWorkstationChange(value) {
            this.searchWorkstation.pageNo = value;
            this.checkListWorkstation();
        },
        // 进行条件搜索
        checkWorkstation() {
            this.checkListWorkstation();
        },
        // 清空搜索框
        cleanWorkstation() {
            this.searchWorkstation.workshopName = ""
            this.searchWorkstation.workstationCode = ""
            this.searchWorkstation.workstationName = ""
            this.searchWorkstation.processName = ""
        },
        // 查询Workshop数据方法
        checkListWorkstation() {

            let qsStr = qs.stringify(this.searchWorkstation);

            this.$axios.post("http://localhost:9970/emsSystem/api/workstation/checkListWorkstation", qsStr).then((res) => {
                if (res.data.code === 200) {
                    this.workstationTable = res.data.data.records
                    this.searchWorkstation.pageNo = res.data.data.current
                    this.searchWorkstation.pageSize = res.data.data.size
                    this.searchWorkstation.total = res.data.data.total
                }
            })
        }
    }
}

</script>
<style>
.divShow {
    background-color: white;
    border-radius: 10px;
}

.rounded-dialog .el-dialog {
    border-radius: 10px;
    /* 设置圆角 */
}
</style>